.TH std::ranges::owning_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::owning_view \- std::ranges::owning_view

.SH Synopsis
   Defined in header <ranges>
   template< ranges::range R >

       requires std::movable<R> && (!/*is-initializer-list*/<R>)  \fI(since C++20)\fP
   class owning_view

       : public ranges::view_interface<owning_view<R>>

   owning_view is a view that has unique ownership of a range. It is move-only and
   stores that range within it.

   The constant /*is-initializer-list*/<R> in the requires-clause is true if and only
   if std::remove_cvref_t<R> is a specialization of std::initializer_list.

.SH Member functions

   constructor   constructs an owning_view by value-initializing or move-constructing
   (C++20)       the stored range
                 \fI(public member function)\fP
   operator=     move-assigns the stored range
   (C++20)       \fI(public member function)\fP
   base          returns a reference to the stored range
   (C++20)       \fI(public member function)\fP
   begin         returns the beginning iterator of the stored range
   (C++20)       \fI(public member function)\fP
   end           returns the sentinel of the stored range
   (C++20)       \fI(public member function)\fP
   empty         checks whether the stored range is empty
   (C++20)       \fI(public member function)\fP
   size          returns the size of the stored sized_range
   (C++20)       \fI(public member function)\fP
   data          returns the pointer to the beginning of the stored contiguous_range
   (C++20)       \fI(public member function)\fP
         Inherited from std::ranges::view_interface
   cbegin        returns a constant iterator to the beginning of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   cend          returns a sentinel for the constant iterator of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   operator bool returns whether the derived view is not empty. Provided if
   (C++20)       ranges::empty is applicable to it.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   front         returns the first element in the derived view. Provided if it
   (C++20)       satisfies forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   back          returns the last element in the derived view. Provided if it satisfies
   (C++20)       bidirectional_range and common_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   operator[]    returns the n^th element in the derived view. Provided if it satisfies
   (C++20)       random_access_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP

std::ranges::owning_view::owning_view

   owning_view() requires std::default_initializable<R> = default; \fB(1)\fP \fI(since C++20)\fP
   owning_view( owning_view&& other ) = default;                   \fB(2)\fP \fI(since C++20)\fP
   constexpr owning_view( R&& t );                                 \fB(3)\fP \fI(since C++20)\fP
   owning_view( const owning_view& ) = delete;                     \fB(4)\fP \fI(since C++20)\fP

   1) Default constructor. Value-initializes the stored range by its default member
   initializer (= R()).
   2) Move constructor. Move constructs the stored range from that of other.
   3) Move constructs the stored range from t.
   4) Copy constructor is deleted. owning_view is move-only.

.SH Parameters

   other - another owning_view to move from
   t     - range to move from

std::ranges::owning_view::operator=

   owning_view& operator=( owning_view&& other ) = default; \fB(1)\fP \fI(since C++20)\fP
   owning_view& operator=( const owning_view& ) = delete;   \fB(2)\fP \fI(since C++20)\fP

   1) Move assignment operator. Move assigns the stored range from that of other.
   2) Copy assignment operator is deleted. owning_view is move-only.

.SH Parameters

   other - another owning_view to move from

.SH Return value

   *this

std::ranges::owning_view::base

   constexpr R& base() & noexcept;               \fB(1)\fP \fI(since C++20)\fP
   constexpr const R& base() const & noexcept;   \fB(2)\fP \fI(since C++20)\fP
   constexpr R&& base() && noexcept;             \fB(3)\fP \fI(since C++20)\fP
   constexpr const R&& base() const && noexcept; \fB(4)\fP \fI(since C++20)\fP

   Returns a reference to the stored range, keeping value category and
   const-qualification.

   1,2) Equivalent to return r_;.
   3,4) Equivalent to return std::move(r_);.

std::ranges::owning_view::begin

   constexpr ranges::iterator_t<R> begin();                      \fB(1)\fP \fI(since C++20)\fP
   constexpr auto begin() const requires ranges::range<const R>; \fB(2)\fP \fI(since C++20)\fP

   Equivalent to return ranges::begin(r_);.

std::ranges::owning_view::end

   constexpr ranges::sentinel_t<R> end();                      \fB(1)\fP \fI(since C++20)\fP
   constexpr auto end() const requires ranges::range<const R>; \fB(2)\fP \fI(since C++20)\fP

   Equivalent to return ranges::end(r_);.

std::ranges::owning_view::empty

   constexpr bool empty() requires requires { ranges::empty(r_); };   \fB(1)\fP \fI(since C++20)\fP
   constexpr bool empty() const requires requires {                   \fB(2)\fP \fI(since C++20)\fP
   ranges::empty(r_); };

   Equivalent to return ranges::empty(r_);.

std::ranges::owning_view::size

   constexpr auto size() requires ranges::sized_range<R>;             \fB(1)\fP \fI(since C++20)\fP
   constexpr auto size() const requires ranges::sized_range<const R>; \fB(2)\fP \fI(since C++20)\fP

   Equivalent to return ranges::size(r_);.

std::ranges::owning_view::data

   constexpr auto data() requires ranges::contiguous_range<R>;        \fB(1)\fP \fI(since C++20)\fP
   constexpr auto data() const requires                               \fB(2)\fP \fI(since C++20)\fP
   ranges::contiguous_range<const R>;

   Equivalent to return ranges::data(r_);.

   Helper templates

   template< class T >

   inline constexpr bool                                                  \fI(since C++20)\fP
   enable_borrowed_range<std::ranges::owning_view<T>> =

       std::ranges::enable_borrowed_range<T>;

   This specialization of std::ranges::enable_borrowed_range makes owning_view satisfy
   borrowed_range when the underlying range satisfies it.

.SH Example


// Run this code

 #include <cassert>
 #include <iostream>
 #include <ranges>
 #include <string>

 int main()
 {
     using namespace std::literals;
     std::ranges::owning_view ov{"cosmos"s}; // the deduced type of R is std::string;
                                             // `ov` is the only owner of this string
     assert(
         ov.empty() == false &&
         ov.size() == 6 &&
         ov.size() == ov.base().size() &&
         ov.front() == 'c' &&
         ov.front() == *ov.begin() &&
         ov.back() == 's' &&
         ov.back() == *(ov.end() - 1) &&
         ov.data() == ov.base()
     );

     std::cout << "sizeof(ov): " << sizeof ov << '\\n' // typically equal to sizeof(R)
               << "range-for: ";
     for (const char ch : ov)
         std::cout << ch;
     std::cout << '\\n';

     std::ranges::owning_view<std::string> ov2;
     assert(ov2.empty());
 //  ov2 = ov; // compile-time error: copy assignment operator is deleted
     ov2 = std::move(ov); // OK
     assert(ov2.size() == 6);
 }

.SH Possible output:

 sizeof(ov): 32
 range-for: cosmos

.SH See also

   ranges::ref_view a view of the elements of some other range
   (C++20)          \fI(class template)\fP
   views::all_t     a view that includes all elements of a range
   views::all       (alias template) (range adaptor object)
   (C++20)
